package com.bwie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.mapper.TypeMapper;
import com.bwie.pojo.Good;
import com.bwie.pojo.Linkage;
import com.bwie.pojo.Type;
import com.bwie.service.GoodService;
import com.bwie.service.LinkageService;
import com.bwie.service.TypeService;
import com.bwie.unit.R;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
public class ListController {

    @Autowired
    private GoodService goodService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private LinkageService linkageService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("pageGood")
    public R pageGood(@RequestBody Good good){
        Page<Good> objectPage = new Page<>(good.getPageNum(), good.getPageSize());
        Page<Good> page = goodService.pageList(objectPage);
        return R.OK(page);
    }

    @PostMapping("typeList")
    public R typeList(){
        List<Type> list = typeService.list();
        return R.OK(list);
    }

    @PostMapping("addOrUpdateGood")
    public R addOrUpdateGood(@RequestBody Good good){
        goodService.saveOrUpdate(good);
        return R.OK();
    }

    @DeleteMapping("deleteGood/{id}")
    public R deleteGood(@PathVariable Integer id){
        goodService.removeById(id);
        return R.OK();
    }

    @PostMapping("linkageList/{id}")
    public R linkageList(@PathVariable Integer id){
        LambdaQueryWrapper<Linkage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Linkage::getLinkageId,id);
        List<Linkage> list = linkageService.list(wrapper);
        return R.OK(list);
    }

//    @PostMapping("collectList/{id}")
//    public R collectList(@PathVariable Integer id){
//        ZSetOperations<String, String> opsForZSet = stringRedisTemplate.opsForZSet();
//        SetOperations<String, String> opsForSet = stringRedisTemplate.opsForSet();
//        Boolean member = opsForSet.isMember("goodId" + id, id + "");
//        if (member){
//            return R.ERROR(500,"不可以重复收藏");
//        }
//        opsForZSet.incrementScore("goodId:count",id+"",1);
//
//    }

}
